package gui;


import java.awt.Font;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import client.LogIn;
import client.Main;
import client.Subscribe;


public class SubscribeGUI extends JFrame{

	Subscribe sub;
	Payment_GUI pay=null;
	
	//panel
	JPanel panel;
	
	//labels
	JLabel info;
	JLabel info1;
	JLabel lblHeader;
	JLabel lblChoice;
	
	//buttons
	public JButton btnBack;
	JButton btnSubscribe;
	
	//radio buttons
	JRadioButton btnrYear;
	JRadioButton btnrMonth;
	ButtonGroup btnGroup;
	
	public SubscribeGUI()
	{
		super();
		init();
	}
	
	JButton getButtonSubscribe()
	{
		if(btnSubscribe==null)
		{
			btnSubscribe=new JButton();
			btnSubscribe.setText("Subscribe");
			btnSubscribe.setBounds(200, 300, 100, 40);
			btnSubscribe.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if( !btnrMonth.isSelected() && !btnrYear.isSelected())
					{
						JOptionPane.showMessageDialog(null, "None of the options selected, Please select one");
						return;
					}
					
					int i=-1;
					 if(btnrMonth.isSelected())
						 i=sub.submit(1);
					 else if(btnrYear.isSelected())
						 i=sub.submit(2);
					 
					 if(i==1)
					 {
						JOptionPane.showMessageDialog(null, "Member subscribed");
						btnBack.doClick();
					 }
					 else if(i==0)
						JOptionPane.showMessageDialog(null, "faild to subscribe");
				}

			});
		}
		return btnSubscribe;
	}
	//info
	JLabel getLabelInfo()
	{
		if(info==null)
		{
			info=new JLabel();
			info.setText(" Discount   Of   ALL   Purchases!!!");
			info.setBounds(120, 120, 370, 20);
			info.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return info;
	}
	JLabel getLabelInfo1()
	{
		if(info1==null)
		{
			info1=new JLabel();
			info1.setText("20% - OFF        10% - OFF");
			info1.setBounds(213, 230, 370, 20);
			info1.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		}
		return info1;
	}
	JLabel getLabelChoice()
	{
		if(lblChoice==null)
		{
			lblChoice=new JLabel();
			lblChoice.setText("Choose:");
			lblChoice.setBounds(97, 198, 100, 20);
			lblChoice.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		}
		return lblChoice;
	}
	
	JRadioButton getRadioYear()
	{
		if(btnrYear==null)
		{
			btnrYear=new JRadioButton("Yearly");
			btnrYear.setBounds(213, 190, 70, 40);
			btnrYear.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		}
		return btnrYear;
	}
	
	JRadioButton getRadioMonth()
	{
		if(btnrMonth==null)
		{
			btnrMonth=new JRadioButton("Monthly");
			btnrMonth.setBounds(310, 190, 85, 40);
			btnrMonth.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,16));
		}
		return btnrMonth;
	}
	
	JButton getButtonBack()
	{
		if(btnBack==null)
		{
			btnBack=new JButton();
			btnBack.setText("Back");
			btnBack.setBounds(380, 10, 100, 40);
		}
		return btnBack;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Member Subscription");
			lblHeader.setBounds(this.getWidth()/2-300/2, 20, 300, 30);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JPanel getPanel()
	{
		if(panel==null)
		{
			panel=new JPanel();
			panel.setSize(this.getWidth(),this.getHeight());
			panel.setLayout(null);
			
			//labels
			panel.add(getLabelHeader());
			panel.add(getLabelChoice());
			panel.add(getLabelInfo());
			panel.add(getLabelInfo1());
			
			//buttons
			panel.add(getButtonBack());
			panel.add(getButtonSubscribe());
			
			//radio buttons
			if(btnGroup==null)
			{
				btnGroup=new ButtonGroup();
				btnGroup.add(getRadioMonth());
				btnGroup.add(getRadioYear());
			}
			panel.add(getRadioMonth());
			panel.add(getRadioYear());
		}
		return panel;
	}
	
	void init()
	{
		sub=new Subscribe(LogIn.usr.getID());
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 500, 400);
		this.setTitle("Zer Lah - Subscription");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.add(getPanel());
		this.setVisible(true);
	}

}
